package spec.zyh;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

//@WebServlet(name = "AddNewServlet")
public class AddNewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
//            String urL="jdbc:mysql://Localhost:3306/1613580003?characterEncoding=utf8";
//            String name="root";
//            String password="123456";
//            Class.forName("com.mysql.jdbc.Driver");
//            Connection com= null;
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            DatabaseConnetion dbc=new DatabaseConnetion();
            Connection com=dbc.getConnection();
            Statement s = null;
            s = com.createStatement();
//            com = DriverManager.getConnection(urL,name,password);
//            Statement s=com.createStatement();
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String sql = "INSERT INTO t_news(title,content) VALUES(\"" + title + "\",\"" + content + "\")";
            s.executeUpdate(sql);
            dbc.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}
